package com.breeze.blockchain.controller.admin;

import com.breeze.blockchain.controller.admin.form.permission.PermissionPageForm;
import com.breeze.blockchain.mybatis.entity.PermissionDo;
import com.breeze.blockchain.service.PermissionService;
import com.breeze.components.web.core.ResponseDTO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 权限 控制层。
 *
 * @author breeze
 * @since 2024-05-11 23:21:51
 */
@RestController
@Tag(name = "权限接口")
@RequestMapping("api/admin/permission")
public class PermissionController {

  @Resource private PermissionService permissionService;

  /**
   * 添加权限。
   *
   * @param permissionDo 权限
   */
  @PostMapping("save")
  @Operation(summary = "保存权限")
  public ResponseDTO<Void> save(@RequestBody @Parameter(name = "权限") PermissionDo permissionDo) {
    permissionService.save(permissionDo);
    return ResponseDTO.ok();
  }

  /**
   * 根据主键删除权限。
   *
   * @param id 主键
   */
  @GetMapping("remove")
  @Operation(summary = "根据主键权限")
  public ResponseDTO<Void> remove(@RequestParam @Parameter(name = "权限主键") Long id) {
    permissionService.removeById(id);
    return ResponseDTO.ok();
  }

  /**
   * 根据主键更新权限。
   *
   * @param permissionDo 权限
   */
  @PostMapping("update")
  @Operation(summary = "根据主键更新权限")
  public ResponseDTO<Void> update(
      @RequestBody @Parameter(name = "权限主键") PermissionDo permissionDo) {
    permissionService.updateById(permissionDo);
    return ResponseDTO.ok();
  }

  /**
   * 查询所有权限。
   *
   * @return 所有数据
   */
  @PostMapping("all")
  @Operation(summary = "查询所有权限")
  public ResponseDTO<List<PermissionDo>> list(
      @RequestBody @Parameter(name = "查询条件") PermissionPageForm form) {
    return ResponseDTO.ok(permissionService.list(form.toQueryWrapper()));
  }

  /**
   * 根据权限主键获取详细信息。
   *
   * @param id 权限主键
   * @return 权限详情
   */
  @GetMapping("detail")
  @Operation(summary = "根据主键获取权限")
  public ResponseDTO<PermissionDo> detail(@RequestParam Long id) {
    return ResponseDTO.ok(permissionService.getById(id));
  }

  /**
   * 分页查询权限。
   *
   * @param form 分页对象
   * @return 分页对象
   */
  @PostMapping("page")
  @Operation(summary = "分页查询权限")
  public ResponseDTO<Page<PermissionDo>> page(
      @RequestBody @Parameter(name = "分页信息") PermissionPageForm form) {
    return ResponseDTO.ok(permissionService.page(form.toPage(), form.toQueryWrapper()));
  }
}
